package com.qk.controller;


import com.qk.anno.Log;
import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController

public class  DeptController{

  @Autowired
    private DeptService deptService;

@Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增部门：{}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping
    public Result listDepts(String name, Integer status,
                            @RequestParam(defaultValue = "1") Integer page ,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        log .info("列表查询：{} {} {} {}", name, status, page, pageSize);


        PageResult<Dept> pageResult = deptService.listDepts(name, status, page, pageSize);
        return Result.success( pageResult);


    }
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {
        log .info("查询id为{}的部门", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);

    }
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("dept:{}", dept);
        deptService.update(dept);
        return Result.success();

    }
    @Log
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("id:{}", id);
        deptService.deleteById(id);
        return Result.success();

    }
    @GetMapping("/list")
    public Result listAll(Integer status) {
        log.info("查询所有部门");
        List<Dept> list = deptService.listAll(status);
        return Result.success(list);
    }




    }






